package com.android.li.temperature.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.li.temperature.bean.ThermalFileNode;
import com.android.li.temperature.impl.IPhoneStatusObserver;
import com.android.li.temperature.util.Constant;
import com.android.li.temperature.util.DateTimeUtil;
import com.android.li.temperature.util.LogUtil;
import com.android.li.temperature.util.ThermalInfoUtil;

public class PhoneStatusManager {

    private static final int MSG_UPDATE = 0;

    private static final int SIZE = ThermalInfoUtil.getThermalSize();

    private String currentTime;

    private List<ThermalFileNode> mTempList;
    public static PhoneStatusManager mInstance;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case MSG_UPDATE:
                Log.d(Constant.TAG, "updateDataSource");
                updateDataSource();
                break;
            default:
                break;
            }
        }
    };

    public static PhoneStatusManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (PhoneStatusManager.class) {
                if (mInstance == null) {
                    mInstance = new PhoneStatusManager(context);
                }
            }
        }
        return mInstance;
    }

    public PhoneStatusManager(Context context) {
        mTempList = new ArrayList<ThermalFileNode>();

        initList();
        writeTypeFile();
    }

    private void initList() {
        BufferedReader brType = null;
        String line = null;

        try {
            for (int i = 0; i < SIZE; i++) {
                ThermalFileNode mFileNode = new ThermalFileNode();

                mFileNode.name = "T" + i;

                mFileNode.pathType = "/sys/class/thermal/thermal_zone" + i + "/type";
                File file = new File(mFileNode.pathType);
                if (null != file && file.canRead()) {
                    brType = new BufferedReader(new FileReader(mFileNode.pathType));
                    if ((line = brType.readLine()) != null) {
                        mFileNode.type = line;
                    }
                } else {
                    mFileNode.type = "unknow";
                }
                
                mTempList.add(mFileNode);
            }

            if (null != brType) {
                brType.close();
            }
        } catch (Exception e) {
            Log.e(Constant.TAG, e.toString());
        }

    }

    private IPhoneStatusObserver mIPhoneStatusObserver;

    public void setIPhoneStatusObserver(IPhoneStatusObserver mIPhoneStatusObserver) {
        this.mIPhoneStatusObserver = mIPhoneStatusObserver;
    }

    public void startDataSource() {
        Log.d(Constant.TAG, "startDataSource");
        LogUtil.delLogFile();
    }

    public void updateMonitor() {
        updateDataSource();
    }

    public void stopDataSource() {
        Log.d(Constant.TAG, "stopDataSource");
        writeTypeFile();
    }

    /**
     * 更新读取到的原始数据
     */
    private void updateDataSource() {
        currentTime = DateTimeUtil.getSysTime();

        BufferedReader brValue = null;
        String line = null;

        try {
            for (int i = 0; i < mTempList.size(); i++) {
                mTempList.get(i).pathTemp = "/sys/class/thermal/thermal_zone" + i + "/temp";
                File mTfile = new File(mTempList.get(i).pathTemp);
                if (null != mTfile && mTfile.canRead()) {
                    brValue = new BufferedReader(new FileReader(mTempList.get(i).pathTemp));
                    if ((line = brValue.readLine()) != null) {
                        mTempList.get(i).value = line;
                    }

                    mTempList.get(i).formatTemparaturValue();
                } else {
                    mTempList.get(i).valueStr = "0";
                }

            }

            if (null != brValue) {
                brValue.close();
            }
        } catch (Exception e) {
            Log.e(Constant.TAG, e.toString());
        }

        updateUI();
    }

    private void updateUI() {

        StringBuilder sbLog = new StringBuilder();
        sbLog.append(currentTime);
        
        StringBuilder sb = new StringBuilder();
        sb.append(currentTime);
        for (ThermalFileNode item : mTempList) {

            sbLog.append(" ");
            sbLog.append(item.valueStr);

            sb.append("\n");
            sb.append(item.toString());
        }

        if (null != mIPhoneStatusObserver) {
            mIPhoneStatusObserver.updateData(sb.toString());
        }

        LogUtil.writeLogFile(sbLog.toString());
    }

    private void writeTypeFile() {
        StringBuilder sb = new StringBuilder();
        sb.append("Time");
        if (mTempList != null && !mTempList.isEmpty()) {
            for (int i = 0; i < mTempList.size(); i++) {
                sb.append(" ");
                sb.append(String.format("T%s(%s)", i, mTempList.get(i).type));
            }

            LogUtil.writeLogTypeFile(sb.toString());
        }

    }

}
